package com.school.admin.common.filter;


import com.school.admin.common.annotation.IgnoreSecurity;
import com.school.admin.common.config.SystemConfig;
import com.school.admin.common.utils.ConversionParametersUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.util.regex.Pattern;

/**
 * get方法拦截器
 */
@Slf4j
public class AuthInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse httpServletResponse, Object handler) throws Exception {

        // 如果不是映射到方法直接通过
        if (!(handler instanceof HandlerMethod)) {
            return true;
        }
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        Method method = handlerMethod.getMethod();



        String requestURI = request.getRequestURI();
        boolean matches = Pattern.matches(SystemConfig.filterAddresses,requestURI);

        //判断是否忽略验证||或者已经被加解密方法给验证过
        if (method.isAnnotationPresent(IgnoreSecurity.class) || matches) {
            return true;
        }
        String sign = request.getHeader("sign");
        return ConversionParametersUtils.parsingSign(handler,sign);

    }



}
